package com.example.library_common.view.video_view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SeekBar;

import androidx.annotation.Nullable;

import com.example.library_common.R;
import com.example.library_common.global.AppGlobals;
import com.example.library_common.utils.QMUIDisplayHelper;
import com.example.library_common.view.video_view.utils.NetInfoModule;
import com.example.library_common.view.video_view.utils.VideoViewControllerListener;
import com.example.library_common.view.video_view.utils.VideoViewUtils;

public class VideoViewControllerView extends FrameLayout {

    private VideoViewCenterControllerView mVideoViewCenterController;

    private VideoViewBottomControllerView mVideoViewBottomController;

    private VideoViewControllerListener mVideoViewControllerListener;

    private View mVideoViewTopController;

    //触摸显示后隐藏的时间
    private int mDismissControlTime = 2500;

    private boolean mPostDismiss = false;


    public void setVideoViewControllerListener(VideoViewControllerListener mVideoViewControllerListener) {

        this.mVideoViewControllerListener = mVideoViewControllerListener;

        if (mVideoViewBottomController != null) {
            mVideoViewBottomController.setVideoViewControllerListener(mVideoViewControllerListener);
        }

        if (mVideoViewCenterController != null) {
            mVideoViewCenterController.setVideoViewControllerListener(mVideoViewControllerListener);
        }

    }

    public VideoViewControllerView(Context context) {
        this(context, null);
    }

    public VideoViewControllerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VideoViewControllerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.video_view_controller_view, this);

        mVideoViewCenterController = findViewById(R.id.mVideoViewCenterController);

        mVideoViewBottomController = findViewById(R.id.mVideoViewBottomController);

        mVideoViewTopController=findViewById(R.id.mVideoViewTopController);

        mVideoViewBottomController.setVisibility(GONE);

    }

    public View getVideoViewTopController() {
        return mVideoViewTopController;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_DOWN:
                mVideoViewBottomController.setVisibility(VISIBLE);
                mVideoViewTopController.setVisibility(VISIBLE);
                break;
            case MotionEvent.ACTION_UP:
                startDismissControlViewTimer();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }


    private void startDismissControlViewTimer() {
        cancelDismissControlViewTimer();
        mPostDismiss = true;
        postDelayed(dismissControlTask, mDismissControlTime);
    }

    private void cancelDismissControlViewTimer() {
        mPostDismiss = false;
        removeCallbacks(dismissControlTask);
    }

    Runnable dismissControlTask = new Runnable() {
        @Override
        public void run() {
            if (mVideoViewControllerListener == null) {
                return;
            }

            int statePlaying = mVideoViewControllerListener.getStatePlaying();
            if (statePlaying != VideoViewControllerListener.STATE_AUTO_COMPLETE
                    && statePlaying != VideoViewControllerListener.STATE_ERROR) {
                if (QMUIDisplayHelper.isLandscape()) {
                    VideoViewUtils.hideNavKey(getContext());
                }
                mVideoViewBottomController.setVisibility(GONE);
                mVideoViewTopController.setVisibility(GONE);
                if (mPostDismiss) {
                    postDelayed(this, mDismissControlTime);
                }
            }
        }
    };

    public void setCoverUrl(String coverUrl) {
        mVideoViewCenterController.setCoverUrl(coverUrl);
    }

    public void showMask(boolean isShowLoading) {
        mVideoViewCenterController.showMask(isShowLoading);
    }

    public void showMaskWhenError() {
        mVideoViewCenterController.showMaskWhenError();
        mVideoViewBottomController.setVisibility(GONE);
    }

    public void onVideoPaying() {
        mVideoViewBottomController.setVisibility(VISIBLE);
        startDismissControlViewTimer();
    }

    public void hidMask() {
        mVideoViewCenterController.hidMask();
    }


    public void startingPlay() {
        if (mVideoViewBottomController != null) {
            mVideoViewBottomController.startingPlay();
        }
    }

    public void stopPlay() {
        if (mVideoViewBottomController != null) {
            mVideoViewBottomController.stopPlay();
        }
    }

    public void setTime(long current, long duration) {
        if (mVideoViewBottomController == null) {
            return;
        }
        mVideoViewBottomController.setTime(current, duration);
    }


    public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener l) {
        if (mVideoViewBottomController == null) return;
        mVideoViewBottomController.setOnSeekBarChangeListener(l);
    }

    public void setProgress(float progress) {
        if (mVideoViewBottomController == null) {
            return;
        }
        mVideoViewBottomController.setProgress(progress);
    }

    public void setBufferProgress(int bufferProgress) {
        if (mVideoViewBottomController == null) {
            return;
        }
        mVideoViewBottomController.setBufferProgress(bufferProgress);
    }

    public ImageView getVideoViewMaskedImg() {
        return mVideoViewCenterController.getVideoViewMaskedImg();
    }
}
